package cn.hxth.dubbo.service.impl;

import cn.hxth.api.model.ProductSpec;
import cn.hxth.api.query.ProductSpecQueryObject;
import cn.hxth.api.service.IProductSpecService;
import cn.hxth.dubbo.service.mapper.ProductSpecMapper;
import cn.hxth.dubbo.service.mq.RabbitMQSender;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ProductSpecServiceImpl implements IProductSpecService {
    @Resource
    private RabbitMQSender rabbitMQSender;
    @Resource
    private ProductSpecMapper productSpecMapper;
    @Resource(name = "myRedisTemplate")
    RedisTemplate<String, Object> redisTemplate;


    @Override
    public boolean insertProductSpec(ProductSpec productSpec) {
        boolean flag = this.productSpecMapper.insert(productSpec);
        if (flag) {
            this.rabbitMQSender.send("data-change-queue", "{\"event_type\": \"add\", \"data_type\": \"product_spec\", \"sid\": " + productSpec.getSid() + ",\"nid\":"+ productSpec.getProductName().getNid()+ "}");
        }
        return flag;
    }

    @Override
    public boolean deleteProductSpec(int sid,int nid) {
        boolean flag = this.productSpecMapper.deleteByPrimaryKey(sid);
        if (flag) {
            this.rabbitMQSender.send("data-change-queue", "{\"event_type\": \"delete\", \"data_type\": \"product_spec\", \"sid\":" + sid+",\"nid\":"+ nid+ "}");
        }
        return flag;
    }


    @Override
    public List<ProductSpec> findAll(int nid) {
        List<ProductSpec> productSpecList = null;
        List<Object> list = this.redisTemplate.opsForHash().values("specList_" + nid);

        if (list == null||list.size()==0) {
            ProductSpecQueryObject productSpecQueryObject = new ProductSpecQueryObject();
            productSpecQueryObject.setNid(nid);
            productSpecList = this.productSpecMapper.selectAll(productSpecQueryObject);
            for (ProductSpec spec : productSpecList) {
                this.redisTemplate.opsForHash().put("specList_" + nid, "spec_" + spec.getSid(), JSONObject.toJSONString(spec));
            }
        } else {
            productSpecList = new ArrayList<>();
            for (Object object : list) {
                String jsonStr = (String) object;
                productSpecList.add(JSONObject.parseObject(jsonStr, ProductSpec.class));
            }
        }
        return productSpecList;

    }

    @Override
    public ProductSpec getProductSpec(int sid) {
        String jsonStr = (String) this.redisTemplate.opsForValue().get("spec_"+sid);
        ProductSpec spec = null ;
        if(jsonStr==null){
            spec  = this.productSpecMapper.selectByPrimaryKey(sid) ;
            this.redisTemplate.opsForValue().set("spec_"+sid,JSONObject.toJSONString(spec));
        }
        else{
            spec = JSONObject.parseObject(jsonStr,ProductSpec.class) ;
        }
        return  spec ;

       /* String jsonStr = this.redisTemplate.opsForHash().get("specList_" + nid, "spec_" + sid).toString();
        if(jsonStr==null){

            this.redisTemplate.opsForHash().put("specList_" + nid, "spec_" + sid, JSONObject.toJSONString(spec));
        }
*/

    }
}
